package fox.framework.rpc.client.message.request;

import fox.framework.rpc.common.StringUtils;
import fox.framework.rpc.converter.RpcMessageConverter;
import fox.framework.rpc.exception.RpcRuntimeException;

/**
 * @author cuichao
 * @Description: RpcClientRequestBuilder
 * @Date: create in 2021/1/22 22:22
 */
public class RpcClientRequestBuilder {

    private RpcClientRequestAPI request = new RpcClientRequestAPI();

    private String protocol;
    private String path;
    private String contentType;
    private RpcMessageConverter converter;
    private Object[] requestBody = new Object[0];


    public void setProtocol(String protocol) {
        this.protocol = protocol;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public void setConverter(RpcMessageConverter converter) {
       this.converter = converter;
    }


    public void setContentType(String contentType) {
      this.contentType = contentType;
    }


    public void setRequestEntityBody(Object[] objects) {
       this.requestBody = objects;
    }

    public RpcClientRequest build() {
        assertRequestProperty();
        request.setPath(path);
        request.setProtocol(protocol);
        request.setContentType(contentType);
        request.setConverter(converter);
        request.setRequestEntityBody(requestBody);
        request.encodeRequestBody();
        return request;
    }



    private void assertRequestProperty(){
        boolean isException = false;
        String message = "";

        if(StringUtils.isEmpty(path)){
            message = "the request path is empty,maybe the rpc-application is not get the method name";
        }
        if(StringUtils.isEmpty(protocol)){
            message = "the request protocol is empty";
        }
        if(StringUtils.isEmpty(contentType)){
            message = "the request content-type is empty";
        }
        if(converter == null){
            message = "the request converter is empty";
        }
        if(isException){
            throw new RpcRuntimeException(message);
        }

    }
}
